/************************************************
 * Author       :   XXY
 * Blog         :   https://www.cnblogs.com/jzyl
 * Gitee        :   https://gitee.com/xiaobaichong
 * Email        :   1047185209@QQ.com
 * FileName     :   Utility.cs
 * CreateData   :   2023/5/13 12:25:11
 * UnityVersion :   2021.3.20f1c1
 * Description  :   实用工具类
************************************************/

using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;

namespace Hotfix
{
    /// <summary>
    /// 实用工具类
    /// </summary>
    internal static class Utility
    {
        /// <summary>
        /// 创建T类型实例
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static T CreateInstance<T>() where T : class
        {
            return Activator.CreateInstance<T>();
        }
        /// <summary>
        /// 设置属性值
        /// </summary>
        /// <param name="obj">实例对象</param>
        /// <param name="name">属性名称</param>
        /// <param name="value">要设置的属性值</param>
        public static void SetPropertyValue(object obj, string name, object value)
        {
            Type type = obj.GetType();
            PropertyInfo property = type.GetProperty(name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            property.SetValue(obj, value);
        }
        /// <summary>
        /// 设置字段值
        /// </summary>
        /// <param name="obj">要设置的对象</param>
        /// <param name="name">设置字段名称</param>
        /// <param name="value">要设置为值</param>
        public static void SetFieldValue(object obj, string name, object value)
        {
            Type type = obj.GetType();
            FieldInfo field = type.GetField(name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            field.SetValue(obj, value);
        }
    }
}
